﻿using System.Web.Mvc;
using SeaBattle.UI.Web.Models;
using SeaBattle.Domain.Abstract.Services;
using SeaBattle.UI.Web.Models.ChildActions;
using System.Linq;
using System;
using SeaBattle.Domain.Abstract;
using SeaBattle.Domain.Entities;
using System.Collections.Generic;

namespace SeaBattle.UI.Web.Controllers
{
    [Authorize]
    public class RoomController : Controller
    {
        private IPlayerService playerService;
        private IAccountService accountService;
        private IGameService gameService;
        private IGameRepository gameRepository;

        public RoomController(IPlayerService playerService, 
            IAccountService accountService, 
            IGameService gameService, IGameRepository gameRepository)
        {
            this.playerService = playerService;
            this.accountService = accountService;
            this.gameService = gameService;
            this.gameRepository = gameRepository;
        }
        //
        // GET: /Entrance/
        public ActionResult Index()
        {
            // Get player's account instance
            var player = playerService.GetPlayer(User.Identity.Name);
            // Ask the account for the balance value
            var balance = accountService.GetPlayerBalance(player);
            
            // Get game list
            var gameList = gameService.GetOpenedGames().Where(x=>x.Creator!=player);
            return View(new RoomViewModel
                            {
                                PlayerAccountBalance = balance,
                                GameList = gameList,
                                Name = player.Name,
                                IsShowYourGames = false
                            });                
        }        

        public ActionResult ShowOwnerGames()
        {
            // Get player's account instance
            var player = playerService.GetPlayer(User.Identity.Name);
            // Ask the account for the balance value
            var balance = accountService.GetPlayerBalance(player);

            // Get game list
            var gameList = gameService.GetOpenedGames().Where(x => x.Creator == player);

            return View(new RoomViewModel
            {
                PlayerAccountBalance = balance,
                GameList = gameList,
                Name = player.Name,
                IsShowYourGames = true
            });              
        }

        [ChildActionOnly]
        public ActionResult GameList()
        {
            // Get player instance
            var player = playerService.GetPlayer(User.Identity.Name);
            // Get all player's games
            var gameList = gameService.GetParticipantsPlayerGames(player)
                .Where(x=>(x.Opponent!=null && x.State != GameState.STATE_CLOSED));
            // 
            return View("ChildActions/GameList", new GameListViewModel { Games = gameList, UserLogin = User.Identity.Name });

        }

        [ChildActionOnly]
        public ActionResult RightPanel()
        {
            return View(new RoomRightPanel());
        }

        [ChildActionOnly]
        [HttpPost]
        public ActionResult RightPanel(RoomRightPanel model)
        {
            if (ModelState.IsValid)
            {
                int preset;
                if (int.TryParse(model.Preset, out preset))
                {
                    if (preset > 0){
                        // Get player's account instance
                        var player = playerService.GetPlayer(User.Identity.Name);
                        // Ask the account for the balance value
                        var balance = accountService.GetPlayerBalance(player);

                        if (balance > preset){
                            var game = gameRepository.CreateNewGame(player,preset);
                            gameRepository.SaveGame(game);
                            accountService.SetPlayerBalance(player, balance - preset);
                            return View(model);
                        }
                        else {
                            ModelState.AddModelError("", "Preset is bigger than your balance!");
                        }
                    }
                    else{
                        ModelState.AddModelError("", "Please, write number bigger than 0");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Please, write number");
                }
            }
            return View(model);
        }

        public string Delete(int GameID)
        {
            gameRepository.DeleteGame(GameID);
            return "Deleted";
        }
    }
}
